package com.example.cglib.client;

import com.example.cglib.service.TimerMethodInterceptor;
import com.example.cglib.service.UserService;
import net.sf.cglib.proxy.Enhancer;

/**
 * ClassName:Client
 * Package:com.example.cglib.client
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/5/10-16:47
 * @Version:v1.0
 */
public class Client {
    public static void main(String[] args) {
        //创建字节码增强器
        Enhancer enhancer = new Enhancer();
        //告诉CGLIB父类是谁,告诉CGLIB目标类是谁
        enhancer.setSuperclass(UserService.class);

        //设置回调(等同于JDK动态代理当中的调用处理器,InvocationHandler)
        enhancer.setCallback(new TimerMethodInterceptor());

        //创建代理对象
        UserService userServiceProxy = (UserService) enhancer.create();
        System.out.println(userServiceProxy);
        //调代理对象的代理方法
        boolean flag = userServiceProxy.login("admin", "123");
        System.out.println(flag?"登录成功":"登录失败");

        userServiceProxy.logout();
    }
}
//com.example.cglib.service.UserService$$EnhancerByCGLIB$$702ebd15@5d3411d